package com.example.qu_tao.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.qu_tao.R;
import com.example.qu_tao.entity.Photo;
import java.util.List;

public class GoodsPhotoAdapter extends RecyclerView.Adapter<GoodsPhotoAdapter.ViewHolder> {

    private static final int ITEM_TYPE_PHOTO = 1;
    private static final int ITEM_TYPE_ADD = 2;
    private List<Photo> mItems;
    private OnPhotoListener mOnPhotoListener;

    public GoodsPhotoAdapter(List<Photo> list) {
        mItems = list;
    }

    public List<Photo> getPhotoList() {
        return mItems;
    }

    public void addPhoto(Photo photo) {
        if (mItems != null && mItems.size() > 0) {
            mItems.add(mItems.size() - 1, photo);
            notifyItemInserted(mItems.size() - 2);
            notifyItemRangeChanged(mItems.size() - 2, mItems.size());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_photo_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (getItemViewType(position) == ITEM_TYPE_ADD) {
            holder.photo.setImageResource(R.mipmap.add_goods_photo);
            holder.delete.setVisibility(View.GONE);
            holder.photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnPhotoListener.onAdd();
                }
            });
        } else {
            Photo photo = mItems.get(position);
            holder.photo.setImageBitmap(photo.getBitmap());
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 检查位置是否合法
                    if (position >= 0 && position < mItems.size()) {
                        mOnPhotoListener.onDelete(position);
                        mItems.remove(position);
                        notifyItemRemoved(position);
                        // 移除 notifyItemRangeChanged 调用
                        // notifyItemRangeChanged(position, mItems.size());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mItems != null && mItems.size() - 1 == position) {
            return ITEM_TYPE_ADD;
        }
        return ITEM_TYPE_PHOTO;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        ImageView delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.goods_photo);
            delete = itemView.findViewById(R.id.delete_photo);
        }
    }

    public void setPhotoListener(OnPhotoListener listener) {
        mOnPhotoListener = listener;
    }

    public interface OnPhotoListener {
        void onAdd();
        void onDelete(int position);
    }
}